package com.wss.module.wan.ui.main.mvp;

import com.orhanobut.logger.Logger;
import com.wss.common.base.mvp.BasePresenter;
import com.wss.common.bean.Template;
import com.wss.common.utils.JsonUtils;
import com.wss.common.utils.ValidUtils;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.bean.BannerInfo;
import com.wss.module.wan.ui.main.mvp.contract.HomeContract;
import com.wss.module.wan.ui.main.mvp.model.HomeModel;
import com.wss.module.wan.ui.project.ProjectAbility;
import com.wss.module.wan.ui.search.SearchAbility;
import com.wss.module.wan.ui.series.SeriesAbility;
import com.wss.module.wan.ui.wxnumber.WXNumberAbility;
import com.zhouyou.http.callback.CallBack;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe：首页Presenter
 * Created by 吴天强 on 2018/10/17.
 */

public class HomePresenter extends BasePresenter<HomeModel, HomeContract.View> implements
        HomeContract.Presenter {

    @Override
    protected HomeModel createModule() {
        return new HomeModel(getLifecycleOwner());
    }

    @Override
    public void start() {
        getBanner();
//        getWxNumber();
        getArticleList();
        getMenuList();
    }

    /**
     * 获取Banner
     */
    private void getBanner() {
        getModel().getBanner(new SimpleCallBack<List<BannerInfo>>() {
            @Override
            public void onError(ApiException e) {
            }

            @Override
            public void onSuccess(List<BannerInfo> bannerInfos) {
                getView().refreshBannerList(bannerInfos);
            }
        });
    }

    /**
     * 获取菜单
     */
    private void getMenuList() {
        List<Template> list = new ArrayList<>();

        list.add(new Template("系列", ResourceTable.Media_wan_icon_3, SeriesAbility.class));
        list.add(new Template("项目", ResourceTable.Media_wan_icon_2, ProjectAbility.class));
        list.add(new Template("公众号", ResourceTable.Media_wan_icon_1, WXNumberAbility.class));
        list.add(new Template("搜索", ResourceTable.Media_wan_icon_4, SearchAbility.class));

        getView().refreshMenuList(list);
    }

    /**
     * 获取微信公众号
     */
    private void getWxNumber() {
//        getModel().getWxNumber().subscribe(wxNumbers -> getView().refreshWxNumber(wxNumbers));
    }


    @Override
    public void getArticleList() {
        getView().showLoading();
        getModel().getArticleList(getView().getPage(), new CallBack<String>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onCompleted() {
                dismissLoading();
            }

            @Override
            public void onError(ApiException e) {
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        dismissLoading();
                        getView().onError("", e.getMessage());
                    }
                },200);
            }

            @Override
            public void onSuccess(String s) {

                String data = JsonUtils.getString(s, "data");
                List<Article> datas = JsonUtils.getList(JsonUtils.getString(data, "datas"), Article.class);

                if (ValidUtils.isValid(datas)) {
                    getView().refreshArticleList(datas);
                } else {
                    getView().onEmpty("");
                }

            }
        });
    }
}
